import 'package:star_author/api/base_api.dart';
import 'package:star_author/api/common_page_load_list_api.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/utils.dart';
import 'package:star_author/views/products/bidding_product_list_item.dart';
import 'package:star_author/widgets/custom_list/common_page_load_controller.dart';
import 'package:star_author/widgets/custom_list/custom_loading_more_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loading_more_list/loading_more_list.dart';

class RelateListTitleWidget extends StatelessWidget {
  const RelateListTitleWidget({Key? key, this.title}) : super(key: key);
  final String? title;

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Container(
        padding: GetPadding.only(top: 20.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image(
              image: AssetImage(
                'images/lt_common/title_line_left.png',
              ),
              width: 98.w,
            ),
            Container(
              margin: GetPadding.only(left: 17.w, right: 17.w),
              child: Text(
                title ?? "相关货品",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16.sp,
                  color: ColorConf.normal,
                ),
              ),
            ),
            Image(
              image: AssetImage(
                'images/lt_common/title_line_right.png',
              ),
              width: 98.w,
            ),
          ],
        ),
      ),
    );
  }
}

class RelateLoadMoreListWidget extends StatefulWidget {
  final Map<String, dynamic> queryParameters;
  final Color? color;
  final String? title;
  final Widget? noMoreChildWidget;
  final BiddingProductListItemStyle? style;
  final BiddingProductListItemFormType? detail;
  final bool isSliver;
  final int count;

  RelateLoadMoreListWidget({
    Key? key,
    required this.queryParameters,
    this.color,
    this.noMoreChildWidget,
    this.style,
    this.detail,
    this.isSliver: true,
    this.title,
    this.count: 10,
  }) : super(key: key);

  _RelateListWidget createState() => _RelateListWidget();
}

class _RelateListWidget extends State<RelateLoadMoreListWidget> {
  late CommonPageLoadController loadController;

  late BaseApi baseApi;

  @override
  void initState() {
    super.initState();
    baseApi = CommonPageLoadListApi(context, '/api/v1/backflows/related')
      ..requestParams = widget.queryParameters;

    loadController = CommonPageLoadController(
      baseApi,
      (jsonRes) {
        return jsonRes;
      },
      count: widget.count,
    );
  }

  @override
  Widget build(BuildContext context) {
    return CustomLoadingMoreList(
      (context, data, int index) {
        return BiddingProductListItem(
          data,
          itemStyle: widget.style ?? BiddingProductListItemStyle.Inside,
          formDetail:
              widget.detail ?? BiddingProductListItemFormType.BiddingDetail,
          refreshPage: (value) {
            loadController[index]['tender_price'] = value;
            loadController[index]['already_tenders_count'] += 1;
            setState(() {});
          },
          isBoxShadow: true,
        );
      },
      loadController,
      extendedListDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 9.w,
        mainAxisSpacing: 9.w,
      ),
      padding: EdgeInsets.only(
        left: 12.w,
        right: 12.w,
        top: 10.w,
      ),
      isSliver: widget.isSliver,
      noMoreChildWidget: widget.noMoreChildWidget ??
          Container(
            alignment: Alignment.center,
            padding: GetPadding.only(top: 20.w, bottom: 15.w),
            child: Text(
              '更多好货去首页逛逛吧～',
              style: TextStyle(
                fontSize: 12.sp,
                color: ColorConf.lightGray,
              ),
            ),
          ),
      shrinkWrap: true,
    );
  }
}
